<!doctype html>
<html lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>言成言成啊</title>
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/meethigher-font.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/css/animate.css">
    <link rel="stylesheet" href="/css/index.css">
</head>
<body>
<header class="header">
    <div class="header-menu"><span class="fa fa-bars"></span></div>
    <div class="header-search"><span class="fa fa-search"></span></div>
    <div class="header-title"><a href="/">言成言成啊 | <span class="header-subtitle">Kit Chen&#39;s Blog</span></a></div>
    <ul class="header-navbar clearFix">
        
            <li><a href="/">主页</a></li>
        
            <li><a href="/tags">标签</a></li>
        
            <li><a href="/archives">归档</a></li>
        
            <li><a href="/search">导航</a></li>
        
    </ul>
</header>
<main class="main">
    <div class="main-content meethigher-scroll">
    <article class="post">
        <div class="post-title">
            <h2 class="title">内部类及其相关知识点</h2>
        </div>
        <div class="post-media2">
            <span class="post-time">2019-10-08</span>
            
                <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li></ul>
            
        </div>
        <div class="post-content blog-markdown">
            <h4 id="1-内部类的概念与分类"><a href="#1-内部类的概念与分类" class="headerlink" title="1. 内部类的概念与分类"></a>1. 内部类的概念与分类</h4><p>如果一个事物的内部包含了另一个事物，那么就是一个类内部包含了另外一个类。</p>
<p>例如：身体和心脏的关系，汽车和发动机的关系。</p>
<p>分类：</p>
<ol>
<li>成员内部类</li>
<li>局部内部类（包含匿名内部类）</li>
</ol>
<h4 id="2-成员内部类的定义及其使用"><a href="#2-成员内部类的定义及其使用" class="headerlink" title="2. 成员内部类的定义及其使用"></a>2. 成员内部类的定义及其使用</h4><p>成员内部类的定义格式：</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">修饰符 <span class="class"><span class="keyword">class</span> 类名称 </span>&#123;</span><br><span class="line">   修饰符 <span class="class"><span class="keyword">class</span> 内部类名称 </span>&#123;</span><br><span class="line">   <span class="comment">//..</span></span><br><span class="line">   &#125;</span><br><span class="line">   <span class="comment">//..</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如何使用成员内部类？两种方式：</p>
<ol>
<li><p>间接方式：在外部类的方法当中，使用内部类；然后main只是调用外部类的方法</p>
</li>
<li><p>直接方式：记住公式</p>
<figure class="highlight haxe"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">类名称 对象名 = <span class="keyword">new</span> <span class="type"></span>类名称();<span class="comment">//一般的对象创建方式</span></span><br><span class="line">外部类名称.内部类名称 对象名 = <span class="keyword">new</span> <span class="type"></span>外部类名称().<span class="keyword">new</span> <span class="type"></span>内部类名称();</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>注意：</p>
<p>内用外，随意访问；</p>
<p>外用内，一定需要借助内部类对象</p>
<p>Body.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Body</span> </span>&#123;<span class="comment">//外部类</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodBody</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"外部类的方法"</span>);</span><br><span class="line">		<span class="keyword">new</span> Heart().beat();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Body</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name=<span class="string">"娃哈哈"</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Body</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name=name;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Heart</span></span>&#123;<span class="comment">//内部类，而且是成员内部类</span></span><br><span class="line">		<span class="comment">//内部类方法</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">beat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"心脏跳动：嘭嘭嘭"</span>);</span><br><span class="line">			System.out.println(<span class="string">"我叫："</span>+name);<span class="comment">//娃哈哈</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">new</span> Body().methodBody();<span class="comment">//间接方式访问</span></span><br><span class="line">		</span><br><span class="line">	<span class="comment">//直接方式访问</span></span><br><span class="line">	<span class="keyword">new</span> Body().new Heart().beat();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果外部类跟内部类的成员变量重复了咋整？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> num=<span class="number">10</span>;<span class="comment">//外部类的成员变量</span></span><br><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Inner</span> /*<span class="keyword">extends</span> <span class="title">Object</span>*/ </span>&#123;<span class="comment">//这个地方表示所有的东西其实都是继承自Object</span></span><br><span class="line">        <span class="keyword">int</span> num=<span class="number">20</span>;<span class="comment">//内部类的成员变量</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodInner</span><span class="params">()</span></span>&#123;</span><br><span class="line">            <span class="keyword">int</span> num=<span class="number">30</span>;<span class="comment">//内部类的局部变量</span></span><br><span class="line">            System.out.println(num);<span class="comment">//30</span></span><br><span class="line">            System.out.println(<span class="keyword">this</span>.num);<span class="comment">//20</span></span><br><span class="line">            System.out.println(Outer.<span class="keyword">this</span>.num);<span class="comment">//30</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果重现重名现象</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">外部类名称.this.外部类成员变量名//访问外部类里面的变量</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">    <span class="keyword">new</span> Outer().new Inner().methodInner();</span><br><span class="line">    <span class="comment">//上下这两个其实是等价的</span></span><br><span class="line">    Outer.Inner inner = <span class="keyword">new</span> Outer().new Inner();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-局部内部类的定义及其使用"><a href="#3-局部内部类的定义及其使用" class="headerlink" title="3. 局部内部类的定义及其使用"></a>3. 局部内部类的定义及其使用</h4><p>如果一个类是定义在一个<strong>方法内部</strong>的，那么这就是一个<strong>局部内部类</strong></p>
<p>局部：只有当前所属的方法才能使用他，出了这个方法外面就不能用了</p>
<p>定义格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">修饰符 class 外部类名称 &#123;</span><br><span class="line"> 修饰符 返回值类型 外部类方法名称（参数列表）&#123;</span><br><span class="line">  class 局部内部类名称 &#123;</span><br><span class="line">   //....</span><br><span class="line">  &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>类的权限修饰符：</p>
<p>public&gt;protected&gt;(default)&gt;private</p>
<p>定义一个类，权限修饰符规则：</p>
<ol>
<li>外部类：只能用public/(default)</li>
<li>成员内部类：public/protected/(default)/private</li>
<li>局部内部类：什么都不写</li>
</ol>
<p>Outer01.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer01</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodOuter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="class"><span class="keyword">class</span> <span class="title">Inner</span> </span>&#123;<span class="comment">//局部内部类</span></span><br><span class="line">			<span class="keyword">int</span> num=<span class="number">10</span>;</span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">innerMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">				System.out.println(num);<span class="comment">//10</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//局部内部类，只有在这个范围内才能使用，所以，看下面</span></span><br><span class="line">		<span class="keyword">new</span> Inner().innerMethod();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">new</span> Outer01().methodOuter();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>局部内部类，如果希望访问所在方法的局部变量，那么这个局部变量必须是<strong>有效final</strong>的</p>
<p>有效final:即使没写final，如果只赋值一次，也是有效final。写上final会更保险，更好</p>
<p>备注：从Java 8开始，只要局部变量不变，那么final就可以省略。</p>
<p>原因：</p>
<ol>
<li>new出来的对象在堆内存中</li>
<li>局部变量是跟着方法走的，在栈内存中</li>
<li>方法运行结束之后，立刻出栈，局部变量就会消失</li>
<li>new出来的对象会在堆中持续存在，直至垃圾回收消失</li>
</ol>
<p>Outer02.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Outer02</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodOuter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> num=<span class="number">10</span>;<span class="comment">//所在方法的局部变量</span></span><br><span class="line">		<span class="comment">//num=20;//如果再加上这一句，就会报错了,因为必须是有效final</span></span><br><span class="line">		<span class="class"><span class="keyword">class</span> <span class="title">Inner</span> </span>&#123;</span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodInner</span><span class="params">()</span> </span>&#123;</span><br><span class="line">				System.out.println(num);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">new</span> Inner().methodInner();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">new</span> Outer02().methodOuter();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-匿名内部类-属于局部内部类-的定义及其使用"><a href="#4-匿名内部类-属于局部内部类-的定义及其使用" class="headerlink" title="4. 匿名内部类(属于局部内部类)的定义及其使用"></a>4. 匿名内部类(属于局部内部类)的定义及其使用</h4><p>如果接口的实现类，或者是父类的子类，只需要<strong>使用唯一的一次</strong>。</p>
<p>那么这种情况下，就可以省略该类的定义，而使用匿名内部类。</p>
<p>匿名内部类的定义格式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">接口名称 对象名 = new 接口名称()&#123;</span><br><span class="line">//覆盖重写接口中所有的抽象方法</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>new 接口名称()表示要继承的接口，后面的{}才是匿名内部类</p>
<p>对格式“new 接口名称(){…}”进行解析：</p>
<ol>
<li>new代表创建对象的动作</li>
<li>接口名称就是匿名内部类要实现的接口</li>
<li>{…}才是匿名内部类的内容</li>
</ol>
<p>注意：</p>
<ol>
<li>匿名内部类，在创建对象的时候，只能使用唯一一次。<br>如果希望多次创建对象，而且类的内容一样的话，那么就必须使用单独定义的实现类了</li>
<li>匿名对象，在调用方法的时候，只能调用唯一一次<br>如果希望同一个对象，调用多次方法，那么必须给对象起名</li>
<li>匿名内部类是省略了实现类或者子类，匿名对象是省略了对象名称<br>强调：匿名内部类和匿名对象不是一回事<br>匿名内部类有对象名称，匿名对象没有</li>
</ol>
<p>MyInterface.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">MyInterface</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span></span>;<span class="comment">//抽象方法，隐藏了public abstract</span></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">method</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>MyInterfaceImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyInterfaceImpl</span> <span class="keyword">implements</span> <span class="title">MyInterface</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"实现类覆盖重写了方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"实现类覆盖重写了方法"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">	<span class="comment">//MyInterfaceImpl impl=new MyInterfaceImpl();</span></span><br><span class="line">	<span class="comment">//MyInterface impl=new MyInterfaceImpl();//多态写法</span></span><br><span class="line">	<span class="comment">//impl.methodAbs();</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	<span class="comment">//可以使用匿名内部类写</span></span><br><span class="line">	MyInterface impl=<span class="keyword">new</span> MyInterface() &#123;</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">		</span><br><span class="line">	impl.methodAbs();</span><br><span class="line">	impl.method();</span><br><span class="line">		</span><br><span class="line">	System.out.println(<span class="string">"==============="</span>);</span><br><span class="line">		</span><br><span class="line">	<span class="comment">//使用了匿名内部类，并且省略了对象名称，也就是匿名对象</span></span><br><span class="line">	<span class="keyword">new</span> MyInterface() &#123;</span><br><span class="line"></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;.methodAbs();</span><br><span class="line">	<span class="comment">//因为匿名对象无法调用第二次方法，所以需要再创建一个匿名内部类的匿名对象</span></span><br><span class="line">	<span class="keyword">new</span> MyInterface() &#123;</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodAbs</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">			System.out.println(<span class="string">"匿名内部类覆盖重写了方法"</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;.method();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-类以及接口作为成员变量"><a href="#5-类以及接口作为成员变量" class="headerlink" title="5. 类以及接口作为成员变量"></a>5. 类以及接口作为成员变量</h4><p>Skill.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Skill</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">useSkill</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>SkillImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SkillImpl</span> <span class="keyword">implements</span> <span class="title">Skill</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">useSkill</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"释放技能：国士无双"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Weapon.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Weapon</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String code;<span class="comment">// 武器代号</span></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Weapon</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Weapon</span><span class="params">(String code)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.code = code;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> code;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCode</span><span class="params">(String code)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.code = code;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Hero.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Hero</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;<span class="comment">// 英雄名字</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;<span class="comment">// 英雄的年龄</span></span><br><span class="line">	<span class="keyword">private</span> Weapon weapon;<span class="comment">// 武器</span></span><br><span class="line">	<span class="keyword">private</span> Skill skill;<span class="comment">//技能</span></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Hero</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Hero</span><span class="params">(String name, <span class="keyword">int</span> age, Weapon weapon,Skill skill)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">		<span class="keyword">this</span>.weapon = weapon;</span><br><span class="line">		<span class="keyword">this</span>.skill=skill;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Weapon <span class="title">getWeapon</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> weapon;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setWeapon</span><span class="params">(Weapon weapon)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.weapon = weapon;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Skill <span class="title">getSkill</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> skill;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSkill</span><span class="params">(Skill skill)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.skill = skill;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">attack</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"英雄"</span>);</span><br><span class="line">		System.out.println(<span class="string">"年龄:"</span>+<span class="keyword">this</span>.getAge()+<span class="string">" 名称:"</span>+<span class="keyword">this</span>.getName()+<span class="string">" 武器:"</span>+<span class="keyword">this</span>.getWeapon().getCode());</span><br><span class="line">		<span class="keyword">this</span>.getSkill().useSkill();</span><br><span class="line">		System.out.println(<span class="string">"Attack enemy！"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Main.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo06Main</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//创建一个英雄角色</span></span><br><span class="line">		Hero hero =<span class="keyword">new</span> Hero();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//为英雄起名、设置年龄</span></span><br><span class="line">		hero.setName(<span class="string">"韩信"</span>);</span><br><span class="line">		hero.setAge(<span class="number">20</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//创建一个Weapon对象</span></span><br><span class="line">		Weapon weapon =<span class="keyword">new</span> Weapon();</span><br><span class="line">		weapon.setCode(<span class="string">"枪"</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//为英雄配备武器</span></span><br><span class="line">		hero.setWeapon(weapon);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置技能：1.可以使用单独定义的实现类 2.可以使用匿名内部类</span></span><br><span class="line">		hero.setSkill(<span class="keyword">new</span> SkillImpl());<span class="comment">//使用单独定义的实现类</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//还可以使用匿名内部类（下面使用的是匿名内部类和匿名对象）</span></span><br><span class="line">		hero.setSkill(<span class="keyword">new</span> Skill() &#123;</span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">useSkill</span><span class="params">()</span> </span>&#123;</span><br><span class="line">				System.out.println(<span class="string">"释放技能：背水一战"</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//英雄Attack</span></span><br><span class="line">		hero.attack();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>
        
            <div class="busuanzi">
                <span id="busuanzi_container_page_pv">阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            </div>
        
    </article>
</div>
    <pointer href="javascript:void(0);" class="arrow fa fa-arrow-up"></pointer>
<pointer href="javascript:void(0);" class="arrow fa fa-arrow-down"></pointer>

</main>
<aside class="aside">
    <div class="aside-content">
        <div class="aside-avatar"><img src="/images/kitchen.jpg" alt="言成"></div>
        <div class="aside-author">言成</div>
        <div class="aside-description">爱学习，爱运动，爱看妹子</div>
        <ul class="aside-contact clearFix">
            <li><span class="fa fa-github"></span><a href="https://github.com/meethigher">Github</a></li>
            <li><span class="fa fa-envelope"></span><a href="javascript:void(0);">meethigher@qq.com</a></li>
            <li><span class="fa fa-weixin"></span><a href="javascript:void(0);"><img src="/images/weixin.png" alt="扫描微信二维码，加我哦！"></a></li>
        </ul>
    </div>
</aside>
<div class="toolbar"><span class="fa fa-th"></span></div>
<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/particles.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@9.0/js/app.js"></script>
<script src="/js/meethigher.js"></script>
<script src="/js/toolbar.js"></script>
</body>
</html>